/**
 * @requires SuperMap/Util.js
 * @requires SuperMap/Layer/CanvasLayer.js
 */

/**
 * Class: SuperMap.Layer.Google 此图层可以访问 google 的影像服务。
 *
 * Inherits from: - <SuperMap.Layer.CanvasLayer>
 */
SuperMap.Layer.Google = SuperMap
    .Class(
    SuperMap.CanvasLayer,
    {

        /**
         * APIProperty: name {String}图层名称，默认为“Google”，防止初始化时未设置图层名
         *
         */
        name: "Google",

        /**
         * Property: url {String}默认的Google影像服务器地址，不需要要用户设置
         */
        url: "http://khms${a}.google.com/kh/v=162&src=app&x=${x}&y=${y}&z=${z}&s=Galileo",

        /**
         * Constructor: SuperMap.Layer.Google
         * 创建google图层，可以浏览google影像 Example: (code)
         *
         * var layer = new SuperMap.Layer.Google("MyName");
         * //将Layer图层加载到Map对象上 map.addLayer(layer);
         * //出图，map.setCenter函数显示地图 //Google图层默认为墨卡托投影，所以定位需要转换
         * map.setCenter( new SuperMap.LonLat(110,39.5 ).transform(
         * new SuperMap.Projection("EPSG:4326"),
         * map.getProjectionObject() ), 4 );
         *
         * (end)
         *
         *
         * Parameters: name - {String} 图层名称
         */
        initialize: function (name, layerType) {
            this.name = name;
            // 设置为墨卡托投影
            var options = {
                projection: "EPSG:900913",
                numZoomLevels: 19
            };
            if (layerType == "vec")
                this.url="http://mt${a}.google.cn/maps/vt?lyrs=m@365036146&hl=zh-CN&gl=CN&&x=${x}&y=${y}&z=${z}";
            else if (layerType == "img")
                this.url ="http://mt${a}.google.cn/maps/vt?lyrs=s%40702&hl=zh-CN&gl=CN&&x=${x}&y=${y}&z=${z}";
            else if (layerType == "label")
                this.url = "http://mt${a}.google.cn/vt/lyrs=h@142&v=162&x=${x}&y=${y}&z=${z}";

           // this.url = this.proxyUrl(this.url);
            SuperMap.CanvasLayer.prototype.initialize.apply(this, [
                name, this.url, {}, options ]);
        },

        proxyUrl: function (url) {
            var purl = BOUNDARY.getBaseUrl() + BOUNDARY.getServiceUrl() + "proxy/tile?url=";
            var _param = url;  //this.url;
            _param = this.replaceChar(_param);
            return purl + _param;
        },

        getBaseUrl: function () {
            var loc = window.location;
            if (loc.origin) {
                return loc.origin;
            }
            return loc.protocol + "//" + loc.host;
        },

        replaceChar: function (url) {
            url = url.replace(/&/g, "$");
            url = url.replace(/\?/g, "!");
            return url;
        },

        /**
         * Method: clone
         */
        clone: function (obj) {
            if (obj == null) {
                obj = new SuperMap.Layer.Google(this.name);
            }
            obj = SuperMap.CanvasLayer.prototype.clone.apply(this,
                [ obj ]);
            return obj;
        },

        /**
         * APIMethod: destroy 解构Google类，释放资源。
         */
        destroy: function () {
            var me = this;
            me.name = null;
            me.url = null;
            SuperMap.CanvasLayer.prototype.destroy.apply(me,
                arguments);
        },
        /**
         * Method: getTileUrl 获取瓦片的URL。
         *
         * Parameters: xyz - {Object} 一组键值对，表示瓦片X, Y, Z方向上的索引。
         *
         * Returns {String} 瓦片的 URL 。
         */
        getTileUrl: function (xyz) {
            var me = this, url;
            url = me.url;
            url = SuperMap.String.format(url, {
                a: Math.round(Math.random() * 3),
                x: xyz.x,
                y: xyz.y,
                z: xyz.z
            });
            return url;
        },
        CLASS_NAME: "SuperMap.Layer.Google"
    });
